#include"queue.h"
queue::queue() : front(nullptr),rear(nullptr){
    //std::cout<<"asdfghjk";
};
void queue::init(){
    this->rear = (qnode_ptr)malloc(sizeof(qnode));
    this->front = this->rear;
    if(!this->front||!this->rear){
        std::cout<<"init fail"<<std::endl;
        exit(-1);
    }
    this->front->next =nullptr;
}
Node* queue::popque(){
    qnode_ptr p;
    if(this->front == this->rear){
        std::cout<<"no elem"<<std::endl;
        exit(-1);
    }
    else{
        p = this->front;
        this->front = this->front->next;
        char c = this->front->data.data;
        free(p);
        return &this->front->data;
    }
}
void queue::pushque(Node n){
    qnode_ptr p = (qnode_ptr)malloc(sizeof(qnode));
    if(!p){
        std::cout<<"push failed"<<std::endl;
        exit(-1);
    }
    p->data = n;
    p->next = nullptr;
    this->rear->next = p;
    this->rear = p;
}

void queue::traverse(){
    for(auto p = this->front->next;p!=nullptr;p=p->next){
        std::cout<<p->data.data;
    }
}
int queue::isempty(){
    //std::cout<<this->front<<" "<<this->rear<<std::endl;
    return this->front == this->rear ? 1 : 0;
}
